// 初始化页面数据
// 查看账单的类型 0-待确认 1-贷结款 2-已结款 3-有问题
var status ="";
var queryStatusArray = ["待确认","已确认","已结款","有问题"];
$(function () {
    initDatagrid();
    // 账单状态，全局
    /*$("#Status").combobox({
        onSelect:function(res){
            status = res.value;
            $("#listTable").datagrid('load',{
                status:status
            })
        }
    });*/
    $("#queryButton").click(function () {
        $("#listTable").datagrid('load', {
            status: $("#Status").combobox('getValue'),
            confirmTime: $("input[name=confirmTime]").val()
        })
    });
});

// 表格内容初始化
function initDatagrid() {
    var url = $.util.getRequestJson('/json/consumeManage.storeBillList.json');
    $('#listTable').datagrid({
        url: url,
        idField:'id',
        fit: true,
        pagination: true,
        pageSize: 30,
        rownumbers:true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        toolbar: '#toolbarButton',
        fitColumns: true,
        columns: [
            [
                {
                    field: 'settlement',
                    title: '结算周期',
                    width:15,
                    align:'center'
                },
                {
                    field: 'sumScore',
                    title: '结算积分',
                    width:15,
                    align:'center'
                },
                // {
                //     field: 'payMoney',
                //     title: '结算金额',
                //     width:15
                // },
                {
                    field: 'status',
                    title: '状态',
                    width:15,
                    align:'center',
                    formatter:function (value,row) {
                        status = value;
                        return queryStatusArray[value];
                    }
                },
                {
                    field: 'confirmTime',
                    title: '确认操作时间',
                    width:20,
                    align:'center',
                    formatter: function (value, row) {
                        return value == null ? "--" : value;
                    }
                },
                {
                    field: 'context',
                    width:20,
                    align:'center',
                    title: '操作',
                    formatter: function (value,row,index) {
                        var aEl = '<a href=javascript:findDetail("{0}","{1}","{2}"); class="easyui-tooltip" title="详情">详情</a>';
                        if(status == 0){
                            //确认账单
                            aEl += ' | <a href=javascript:confirmBill("{1}"); class="easyui-tooltip" title="确认账单">确认</a>';
                        }
                        //格式化操作按钮
                        return $.string.format(aEl,index,row.id,row.status);
                    }
                }
            ]
        ]
    });
}


// 查看账单详情
function findDetail(index,billId,status) {
    if(status==0){
        buttons=[{
            text: '确认',
            handler: function () {
                    parent.$.messager.confirm('确认账单？', function(r){
                        if (r){
                            $.util.get($.util.getRequestJson('/json/consumeManage.confirmBill.json'),{billId:billId},function(data){
                                if(data.success){
                                    $('#listTable').datagrid('reload');
                                    dialog.dialog('close');
                                }
                            });
                        }
                    });

            }
        }];

    }else{
        buttons=[];
    }
    var title = "账单详情";

    var url = $.util.getRootPath() + '/page/shopManager/billConfirm/billDetail.html?billId='+billId;
    var dialog = $.easyui.showDialog({
        iconCls:"",
        title: title,
        width: 900,
        height:550,
        maximizable: true,
        href: url,
        topMost: true,
        enableSaveButton: false,
        closeButtonText: '关闭',
        buttons: buttons
    });
}



// 确认账单
function confirmBill(billId) {
    $.messager.confirm('确认账单？', function(r){
        if (r){
            $.util.get($.util.getRequestJson('/json/consumeManage.confirmBill.json'),{billId:billId},function(data){
                if(data.success){
                    $('#listTable').datagrid('reload');
                }
            });
        }
    });


}

